import { Component } from '@angular/core';
import { AlertController ,IonicPage, NavController,  Platform, NavParams } from 'ionic-angular';

import {CommonUtilService} from "../../providers/commonUtilService";
import {UtilService} from "../../providers/utilService";
import {UrlService} from "../../providers/urlService";
import {ParamService} from "../../providers/paramService";
import {IntfService} from '../../providers/intfService';

/**
 * Generated class for the TabsServicePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

declare var DateUtil: any;

@IonicPage()
@Component({
  selector: 'page-tabs-mycenter',
  templateUrl: 'tabs-mycenter.html',
})
export class MyCenterPage {
  dateseltype:any=2;
  tasklistjsondata:any;//任务列表数据
  items:any;//任务列表数据
  loading: any; //页面loading框变量
  title:any='本月任务列表';//标题
  constructor(
    public commonUtil:CommonUtilService,
    public navCtrl: NavController ,
    public navParams: NavParams,
    public intfService: IntfService
  ) { }

  ionViewDidLoad() {
    console.log('ionViewDidLoad TabsTaskPage');
    this.querytasklist();
  }
  openModal() {
    let itemlist: any = UtilService.getDateSellist();
    let radioAlertParams: any = {};
    radioAlertParams.title = "日期选择";
    radioAlertParams.inputList = itemlist;
    radioAlertParams.cancelCallback = () => {
      console.log('取消');
    }
    radioAlertParams.successCallback = (data) => {
      if (data ==  this.dateseltype) {
        console.log('我啥都没选');
        return;
      }


      this.title=itemlist[ data].label +'任务列表';
      this.dateseltype = data;
      console.log('你选择的日期是：'+data);
      this.querytasklist();//刷新任务列表
    }
    this.commonUtil.showRadioAlert(radioAlertParams);
  }



  //获取任务列表
  querytasklist() {
    console.log('获取任务列表:querytasklist');
    let params = {
      "Depart_Code": "-1",
      "User_ID": "-1",
      "Task_ID": "-1",
      "top": "20",
      "order": "IN_Date desc",
      "page": 1,
      "fields": "Task_ID,Task_Name,IN_Date,Manage_Name",
      "seldatetype": this.dateseltype,
      "selstate": "count",
      "Cust_Phone": ""
    }
    this.loading =  this.commonUtil.showLoading({'content':'获取任务...', 'enableBackdropDismiss':true, 'dismissOnPageChange':false, 'duration':10000});

    this.intfService.getReq(UrlService.url_task(), params, true).then((data) => {
      console.log(JSON.stringify(data));

      for (var i = 0; i < data.length; i++) {
        data[i].IN_Date = DateUtil.timestamp2Time(data[i].IN_Date);
      }
      this.tasklistjsondata = data ;//成员类型
      this.items=data;
      this.loading.dismiss();
    }).catch((error) => {
      this.loading.dismiss();
      this.commonUtil.showAlert({title:'温馨提示',message:'获取任务列表失败！'});
      console.log("获取任务列表失败:"+JSON.stringify(error));
    });

  }


  //筛选
  getItems(ev) {
    this.items=this.tasklistjsondata;
    var val = ev.target.value;
    if (val && val.trim() != '') {
      this.items = this.items.filter((item) => {
        return (item.Task_Name.trim().indexOf(val) > -1);
      })
    }
  }

  //跳转到明细页面
  itemSelected(info) {
    this.navCtrl.push('TaskPage', info);
  }


  //跳转到页面
  toPage(pageName: any) {
    this.navCtrl.push(pageName, {
    });
  }
}

